package org.liuqm.common;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.net.ContentHandler;
import java.util.stream.Collectors;

public class ParameterUtils {

    /**
     * 根据请求路径调用相应的方法
     *
     * @param target
     * @param url
     * @param request
     * @return
     */
    public static AjaxResult invoke(Object target, String url, HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        Method currentMethod = getMethod(target.getClass(), url);
        if (currentMethod == null) {
            ajaxResult.setCode(404);
            ajaxResult.setMsg("您访问的资源不存在！");
            return ajaxResult;
        }
        ajaxResult.setCode(500);
        Object[] values = getParameters(currentMethod, request);
        try {
            Object result = currentMethod.invoke(target, values);
            if (result instanceof AjaxResult) {
                ajaxResult = (AjaxResult) result;
            } else {
                ajaxResult.setCode(200);
                ajaxResult.setResult(result);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }


        return ajaxResult;


    }





    private static Object[] getParameters(Method method, HttpServletRequest request) {

        Parameter[] parameters = method.getParameters();
        Object[] values = new Object[parameters.length];
        String contentType=request.getHeader("Content-Type");
        try {
            String params = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        contentType.equals("application/json");

        for (int i = 0; i < parameters.length; i++) {
            Parameter parameter = parameters[i];
            RequestParam requestParam = parameter.getAnnotation(RequestParam.class);
            String paramName = requestParam.value();





        }

        return values;
    }

    /**
     * 通过路径匹配方法
     *
     * @param clazz
     * @param url
     * @return
     */
    private static Method getMethod(Class clazz, String url) {
        Method[] methods = clazz.getMethods();
        String urlStr = "";
        RequestMapping classAnno = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
        if (classAnno != null) {
            urlStr = classAnno.value();
        }
        Method currentMethod = null;
        for (Method method : methods) {
            RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
            if (methodAnnotation != null) {
                urlStr += methodAnnotation.value();
            }
            if (url.equals(urlStr)) {
                currentMethod = method;
            }
        }
        return currentMethod;
    }

}
